package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.MajorTypeDao;
import common.domain.MajorType;
import common.domain.Role;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MajorTypeDaoImpl implements MajorTypeDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(MajorType majorType) {
        String sql = "insert into majorType\n" +
                "        (majorType_id,majorType_name) \n" +
                "values(?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,majorType.getMajorType_id());
            psmt.setString(2,majorType.getMajorType_name());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(MajorType majorType) {
        String sql = "update majorType set";
        List<Object> param = new ArrayList<>();
        if (majorType == null){
            return;
        }else{
            if(majorType.getMajorType_id() != null){
                sql += " majorType_id = ?,";
                param.add(majorType.getMajorType_id());
            }
            if(majorType.getMajorType_name() != null){
                sql += " majorType_name = ?,";
                param.add(majorType.getMajorType_name());
            }

        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where majorType_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,majorType.getMajorType_id());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long majorType_id) {
        String sql = "delete from majorType where majorType_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,majorType_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public MajorType selectById(Long majorType_id) {
        String sql = "select * from majorType where majorType_id = ?";
        MajorType majorType = new MajorType();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,majorType_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                majorType.setMajorType_id(rs.getLong(1));
                majorType.setMajorType_name(rs.getString(2));

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return majorType;
    }

    @Override
    public List<MajorType> selectList(MajorType majorType) {
        String sql = "select * from majorTye where 1=1";
        List<MajorType> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (majorType == null){
            sql = "select * from majorType";
        }else{
            if(majorType.getMajorType_id() != null){
                sql += " and majorType_id = ?";
                param.add(majorType.getMajorType_id());
            }
            if(majorType.getMajorType_name() != null){
                sql += " and majorType_name like \"%\"?\"%\"";
                param.add(majorType.getMajorType_name());
            }

        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                MajorType majorType1 = new MajorType();
                majorType1.setMajorType_id(rs.getLong(1));
                majorType1.setMajorType_name(rs.getString(2));

                list.add(majorType1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
